package com.dragon.flow.resource.base;

import com.dragon.flow.model.base.Category;
import com.dragon.flow.service.base.FlowablePlusCategoryService;
import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.pager.PagerModel;
import com.dragon.tools.pager.ParamVo;
import com.dragon.tools.vo.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title:
 * @author: bruce.liu
 * @since: 2024/6/16 16:56
 */
@RestController
@RequestMapping("/flow/base/category")
public class CategoryResource {

    @Autowired
    private FlowablePlusCategoryService flowablePlusCategoryService;

    /**
     * 查询下拉列表集合
     *
     * @return
     */
    @PostMapping(value = "/getCategories", produces = "application/json")
    public ReturnVo<List<Category>> getCategories(@RequestBody Category category) {
        ReturnVo<List<Category>> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        List<Category> list = flowablePlusCategoryService.getCategories(category);
        returnVo.setData(list);
        return returnVo;
    }

    /**
     * 分页获取流程分类列表
     *
     * @param params 查询参数
     * @return
     */
    @PostMapping(value = "/getPagerModel", produces = "application/json")
    public ReturnVo<PagerModel> getPagerModel(@RequestBody ParamVo<Category> params) {
        ReturnVo<PagerModel> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        PagerModel<Category> pm = flowablePlusCategoryService.getPagerModelByWrapper(params.getEntity(), params.getQuery());
        returnVo.setData(pm);
        return returnVo;
    }

    /**
     * 添加或者修改
     *
     * @param category 参数
     * @return
     */
    @PostMapping(value = "/saveOrUpdate", produces = "application/json")
    public ReturnVo<String> saveOrUpdate(@RequestBody Category category) {
        ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.FAIL, "操作失败！");
        flowablePlusCategoryService.saveOrUpdateCategory(category);
        returnVo.setCode(ReturnCode.SUCCESS);
        returnVo.setMsg("操作成功");
        return returnVo;
    }

    /**
     * 批量删除信息
     *
     * @param appIds
     * @return
     */
    @PostMapping(value = "/delete", produces = "application/json")
    public ReturnVo<String> delete(@RequestBody List<String> appIds) {
        return flowablePlusCategoryService.deleteByIds(appIds);
    }

    /**
     * 查询编码查询对象
     *
     * @return
     */
    @PostMapping(value = "/getCategoryByCode/{id}", produces = "application/json")
    public ReturnVo<Category> getCategoryByCode(@PathVariable String id) {
        ReturnVo<Category> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
        Category getCategory = flowablePlusCategoryService.getCategoryByCode(id);
        returnVo.setData(getCategory);
        return returnVo;
    }

}
